using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Acms.Bll;
using System.Collections.Generic;
using Telerik.Web.UI;

public partial class Users_Roles : BaseUserPage
{
    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.MembershipAdministrator, Role.Roles.MembershipViewer }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridColumn edit = gridRoles.Columns.FindByUniqueName("Edit");
            if (edit != null)
            {
                edit.Visible = CanEdit();
            }
            GridColumn delete = gridRoles.Columns.FindByUniqueName("Delete");
            if (delete != null)
            {
                delete.Visible = CanDelete();
            }
        }

    }
    
    protected void dsUserRoles_SelectData(object sender, Acms.DataControls.SelectEventArgs<UserRole> e)
    {
        List<UserRole> roles = UserRole.GetAllActiveByUserIdSiteId(base.CurrentUserId, AcmsContext.SiteId);
        e.SetData(roles);
    }

    protected void dsRoles_SelectData(object sender, Acms.DataControls.SelectEventArgs<Role> e)
    {
        List<Role> roles = Role.GetAllBySiteId(AcmsContext.SiteId);
        e.SetData(roles);
    }
    protected void gridRoles_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if(e.Item.ItemType == Telerik.Web.UI.GridItemType.EditFormItem)
        {
            DropDownList ddlRole = e.Item.FindControl("ddlRole") as DropDownList;
            RadDateTimePicker txtStart = e.Item.FindControl("txtStart") as RadDateTimePicker;
            RadDateTimePicker txtEnd = e.Item.FindControl("txtEnd") as RadDateTimePicker;
            if (e.Item is GridEditFormInsertItem)
            {

                txtStart.SelectedDate = DateTime.Now;
                txtStart.MinDate = DateTime.Now;
                txtEnd.MinDate = DateTime.Now;
            }
            else if(e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                UserRole userRole = e.Item.DataItem as UserRole;
                if (userRole != null)
                {
                    ddlRole.SelectedValue = userRole.RoleId.ToString();

                    txtStart.MinDate = userRole.Start;
                    txtStart.SelectedDate = userRole.Start;

                    if (userRole.Start <= DateTime.Today)
                        txtStart.Enabled = false;
                    else
                    {
                        txtStart.MinDate = DateTime.Now;
                        txtStart.Enabled = true;
                    }
                    if (userRole.End.HasValue)
                        txtEnd.SelectedDate = userRole.End.Value;
                    txtEnd.MinDate = DateTime.Now;

                }
            }
        }
    }

    protected void gridRoles_InsertCommand(object source, GridCommandEventArgs e)
    {
    }

    protected void dsUserRoles_BeforeUpdating(object sender, Acms.DataControls.BeforeUpdatingEventArgs e)
    {

        //Find the relative controls
        GridEditableItem item = null;
        if(e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            item = gridRoles.MasterTableView.GetInsertItem();
        }
        else
        {
            GridItem[] list = gridRoles.MasterTableView.GetItems(GridItemType.EditFormItem);
            foreach(GridEditableItem tmp in list)
            {
                if(tmp.IsInEditMode)
                {
                    item = tmp;
                    break;
                }
            }
        }

        if(item != null)
        {
            DropDownList ddlRole = item.FindControl("ddlRole") as DropDownList;
            RadDateTimePicker txtStart = item.FindControl("txtStart") as RadDateTimePicker;
            RadDateTimePicker txtEnd = item.FindControl("txtEnd") as RadDateTimePicker;

            if (e.Action == Acms.DataControls.UpdateAction.Insert)
            {
                e.Values.Add("UserId", CurrentUserId);
                e.Values.Add("RoleId", ddlRole.SelectedValue);
                e.Values.Add("SiteId", AcmsContext.SiteId);
            }

            if (e.Action == Acms.DataControls.UpdateAction.Insert || e.Action == Acms.DataControls.UpdateAction.Update)
            {
                e.Values.Add("Start", txtStart.SelectedDate);
                e.Values.Add("End", txtEnd.SelectedDate);
            }

        }

    }

    public bool CanEdit()
    {
        if (User.Identity.User.Id != CurrentUserId)
        {
            return User.HasRole(new Role.Roles[] { Role.Roles.Developer, Role.Roles.MembershipAdministrator });
        }
        else
        {
            return false;
        }
    }

    public bool CanDelete()
    {
        if (User.Identity.User.Id != CurrentUserId)
        {
            return User.HasRole(new Role.Roles[] { Role.Roles.Developer, Role.Roles.MembershipAdministrator });
        }
        else
        {
            return false;
        }
    }
}
